package cn.itcast.util;


import cn.itcast.avro.SseAvro;
import cn.itcast.avro.SzseAvro;
import cn.itcast.bean.CleanBean;
import cn.itcast.config.QuotConfig;
import cn.itcast.constant.Constant;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
//时间过滤
public class QuotUtil {
    private static final Logger LOG = LoggerFactory.getLogger(QuotUtil.class);

    private static final FastDateFormat df = FastDateFormat.getInstance(Constant.format_YYYYMMDDHHMMSS);

    public static String getTradeDate() {

        String date = QuotConfig.config.getProperty("proc.date");
        if (date == null) {
            date = df.format(new Date()).substring(0, 8);
        }
        return date;
    }

    /**
     * 过滤指数数据
     */
    public static boolean isIndex(CleanBean cleanBean) {
        return cleanBean.getMdStreamId().equals("MD001") || cleanBean.getMdStreamId().equals("900");
    }

    /**
     * 过滤个股数据
     */
    public static boolean isStock(CleanBean CleanBean) {
        return "MD002".equals(CleanBean.getMdStreamId()) || "010".equals(CleanBean.getMdStreamId());
    }

    /**
     * 时间过滤：指非交易时间段数据
     */
    public static boolean checkTime(Object line) {
        Boolean boolTime = false;
        if (line instanceof SseAvro) {
            Long timestamp = ((SseAvro) line).getTimestamp();
            if(timestamp != null){
                boolTime = SpecialTimeUtil.openTime <= timestamp && SpecialTimeUtil.closeTime >= timestamp;
            }

        } else {
            Long timestamp = ((SzseAvro) line).getTimestamp();
            boolTime = SpecialTimeUtil.openTime <= timestamp && SpecialTimeUtil.closeTime >= timestamp;
        }
        return boolTime;
    }

    /**
     * 数据过滤：指高开低收为null或者是0的数据
     * 校验 开盘价,最高价,最低价,最新价 是否为 0
     */
    public static boolean checkData(Object object) {
        Boolean boolData = false;
        if (object instanceof SseAvro) {
            return !(((SseAvro) object).getTradePrice() == 0
                    || ((SseAvro) object).getHighPrice() == 0
                    || ((SseAvro) object).getLowPrice() == 0
                    || ((SseAvro) object).getOpenPrice() == 0);

        } else if (object instanceof SzseAvro) {
            return !(((SzseAvro) object).getTradePrice() == 0
                    || ((SzseAvro) object).getHighPrice() == 0
                    || ((SzseAvro) object).getLowPrice() == 0
                    || ((SzseAvro) object).getOpenPrice() == 0);
        }
        return boolData;
    }


}
